<template>
  <div class="shell">
    <el-card class="box-card">
      <!--标题 -->
      <div slot="header" class="clearfix">
        <span>电影推荐系统</span>
      </div>

      <!--登录 -->
      <el-tabs v-model="currentIndex" stretch @tab-click="handleClick">
        <el-tab-pane label="登录" name="login">
          <el-form :model="loginForm" :rules="rules" status-icon ref="loginForm"
                   @keyup.enter.native="submitForm('loginForm')">
            <!--@keyup.enter.native="submitForm('registerForm') 回车自动提交事件-->
            <!--用户名 -->
            <el-form-item label="登录账户" prop="username" label-width="80px">
              <el-input type="text" v-model="loginForm.username" auto-complete="off"></el-input>
            </el-form-item>
            <!--密码 -->
            <el-form-item label="密码" prop="password" label-width="80px">
              <el-input type="password" v-model="loginForm.password" auto-complete="off"></el-input>
            </el-form-item>
            <!--提交 -->
            <el-form-item>
              <el-button type="primary" @click="submitForm('loginForm')" :disabled="!loginCanSubmit">提交</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>

        <!--注册 -->
        <el-tab-pane label="注册" name="register">
          <el-form :model="registerForm" :rules="rules" status-icon ref="registerForm"
                   @keyup.enter.native="submitForm('registerForm')">
            <!--@keyup.enter.native="submitForm('registerForm') 回车自动提交事件-->
            <!-- 用户名 -->
            <el-form-item label="登录账户" prop="username" label-width="80px">
              <el-input type="text" v-model="registerForm.username" auto-complete="off"></el-input>
            </el-form-item>
            <!-- 昵称 -->
            <el-form-item label="昵称" prop="username" label-width="80px">
              <el-input type="text" v-model="registerForm.nickName" auto-complete="off"></el-input>
            </el-form-item>
            <!-- 密码 -->
            <el-form-item label="密码" prop="password" label-width="80px">
              <el-input type="password" v-model="registerForm.password" auto-complete="off"></el-input>
            </el-form-item>
            <!-- 确认密码 -->
            <el-form-item label="确认密码" prop="configurePassword" label-width="80px">
              <el-input type="password" v-model="registerForm.configurePassword" auto-complete="off"></el-input>
            </el-form-item>
            <!-- 手机号 -->
            <el-form-item label="手机号" prop="phonenumber" label-width="80px">
              <el-input type="text" v-model="registerForm.phonenumber" auto-complete="off"></el-input>
            </el-form-item>
            <!-- 邮箱 -->
            <el-form-item label="邮箱" prop="email" label-width="80px">
              <el-input type="text" v-model="registerForm.email" auto-complete="off"></el-input>
            </el-form-item>
            <!-- 提交 -->
            <el-form-item>
              <el-button type="primary" @click="submitForm('registerForm')" :disabled="!registerCanSubmit">提交
              </el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>

      </el-tabs>
    </el-card>
  </div>
</template>


<script>
import axios from 'axios';

export default {
  data() {
    // 用户名验证规则
    var validateUserName = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('用户名不能为空'));
      } else {
        callback();
      }
    }

    // 密码验证规则
    var validatePassWord = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        callback();
      }
    }

    // 确认密码验证规则
    var validateConfigurePassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else if (value !== this.registerForm.password) {
        callback(new Error('两次密码不一致'))
      } else {
        callback();
      }
    }

    // 返回数据
    return {
      currentIndex: "login",
      loginForm: {
        username: "",
        password: "",
      },
      registerForm: {
        username: "",
        password: "",
        configurePassword: ""
      },
      activeTab: "login",
      loginResultData: [],
      registerResultData: [],

      // 规则
      rules: {
        username: [
          {validator: validateUserName, trigger: 'blur'}
        ],
        password: [
          {validator: validatePassWord, trigger: 'blur'}
        ],
        configurePassword: [
          {validator: validateConfigurePassword, trigger: 'blur'}
        ]
      }
    };
  },

  // 计算
  computed: {
    loginCanSubmit() {
      const {username, password} = this.loginForm
      return Boolean(username && password)
    },
    registerCanSubmit() {
      const {username, password, configurePassword} = this.registerForm
      return Boolean(username && password && configurePassword)
    }
  },

  // 方法
  methods: {
    // 提交按钮事件
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        // const _this = this;
        if (valid) {
          if (this.activeTab === "login") {
            // 登录
            console.log("登录提交信息:", this.loginForm);    //显示提交内容
            // 调用登录接口
            axios.post("http://localhost:8080/login", this.loginForm).then((loginResult) => {
              this.loginResultData = loginResult.data;  //接收结果
              console.log("这是注册loginResultData的结果:", this.loginResultData)
              // 判断是否登录成功
              if (this.loginResultData.code === 0) {
                // 登录失败
                this.$message({
                  message: '登录失败,用户名或密码错误',
                  type: 'error'
                });
              } else {
                // 登录成功
                localStorage.setItem('Authorization', 'Bearer ' + this.loginResultData.token)
                // localStorage.setItem('username', this.loginResultData.data.username)
                // 跳转页面
                this.$router.push({
                  path: '/',
                })
              }
            });
          } else if (this.activeTab === "register") {
            // 注册
            console.log("这是注册提交的信息:", this.registerForm);   //显示提交内容
            axios.post('http://localhost:8080/register', this.registerForm).then((registerResult) => {
              this.registerResultData = registerResult.data;  //接收结果
              console.log("这是注册registerResultData的结果:", this.registerResultData)
              if (this.registerResultData.code === 1) {
                // 注册成功提示信息
                this.$message({
                  message: '注册成功,请返回登录',
                  type: 'success'
                });
              } else {
                this.$message({
                  message: '注册失败,用户名重复',
                  type: 'error'
                });
              }
            });

          }
        }
      });
    },
    // Tabs切换点击事件
    handleClick(tab) {
      this.activeTab = tab.name
    }
  },
};
</script>


<style>
.shell {
  width: 1200px;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 0 auto;
}

.box-card {
  width: 500px;
  justify-content: center;
  align-content: center;
  margin: 0px auto;
  text-align: center;
}

.box-card .clearfix span {
  font-size: 24px; /* 设置标题字体大小为 24 像素 */
}
</style>
